package com.home.duy.GUI;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

import com.home.Common.Common;
import com.home.Common.Resources_Duy;
import com.home.Entity.QuanCoffee;
import com.home.Validation.Validator;
import com.home.duy.code.DAO.QuanCoffeeDAO;
import com.home.duy.code.FileFilter.ImageFileFilter;
import com.home.duy.code.components.ImageFileView;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JToolBar;

public class ThongTinQuan extends JInternalFrame {

	private ParentFrame parent;
	private QuanCoffeeDAO quanDao = null; // @jve:decl-index=0:
	private QuanCoffee quan = null;
	private BufferedImage imgCurrent = null;
	private String extension = ""; // @jve:decl-index=0:
	private static final int MAX_LENGHT_TEN = 20;
	private static final int MAX_LENGHT_DIACHI = 60;
	private static final int WIDTH_LOGO = 128;
	private static final int HEIGHT_LOGO = 128;
	private JPanel jContentPane = null;
	private JPanel panel = null;
	private JPanel panelImg = null;
	private JLabel jLabel = null;
	private JLabel imgLogo = null;
	private JPanel jPanel = null;
	private JButton btnChange = null;
	private JTextArea txtTen = null;
	private JTextField txtMST = null;
	private JTextField txtDiaChi = null;
	private JTextField txtSDT = null;
	private JToggleButton btnEdit = null;
	private JPanel panelMST = null;
	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;

	// @jve:decl-index=0:
	/**
	 * This is the xxx default constructor
	 */
	public ThongTinQuan(ParentFrame parent) {
		super();
		this.parent = parent;
		initialize();
		quanDao = new QuanCoffeeDAO();
		quan = (QuanCoffee) quanDao.findByPk(QuanCoffee.class, 1);
		fillData(quan);
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 440);
		//this.setBounds(Common.getBoundsChildFrame(parent.getWidthDesktopPane(),parent.getHeightDesktopPane(),600, 500));
		this.setContentPane(getJContentPane());
		this.setTitle(Resources_Duy.TITLE_MENU_ITEM_THONGTIN);
		this.setClosable(true);
		this.setIconifiable(true);
	}

	// ====================== PROCESS METHOD ==========================

	private void fillData(QuanCoffee quan) {
		Font fontTen = new Font("Algerian", Font.BOLD, 36);
		txtTen.setFont(fontTen);
		txtTen.setText(quan.getTen());
		txtMST.setText(quan.getMst());
		txtDiaChi.setText(quan.getDiaChi());
		txtSDT.setText(quan.getSdt());
		ImageIcon img = null;
		try {
			if(Validator.isNotBlank(quan.getLogo()))
			{
			File f = new File(Resources_Duy.PATH_SAVE_IMAGE
					+ quan.getLogo());
			if (f.exists())
			{
					BufferedImage buffImg = Common.getBufferImage(f);
					img = Common.convertBufferImageToImageIcon(buffImg);
			}
			else
				img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_LOGO);
			}
			else
			{
				img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_LOGO);
			}
		} catch (Exception e) {
			img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_LOGO);
			e.printStackTrace();
		}
		imgLogo.setIcon(img);
	}

	private boolean validation() {
		boolean check = true;
		if (Validator.isBlank(txtTen.getText())
				|| txtTen.getText().length() > MAX_LENGHT_TEN) {
			check = false;
			JOptionPane.showMessageDialog(this, "Tên Quán Không Hợp Lệ !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtTen.requestFocus();
		} else if (Validator.isBlank(txtMST.getText())
				|| txtMST.getText().length() > 20) {
			check = false;
			JOptionPane.showMessageDialog(this, "Mã Số Thuế Không Hợp Lệ !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtMST.requestFocus();
		} else if (Validator.isBlank(txtDiaChi.getText())
				|| txtDiaChi.getText().length() > MAX_LENGHT_DIACHI) {
			check = false;
			JOptionPane.showMessageDialog(this, "Địa Chỉ Không Hợp Lệ !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtDiaChi.requestFocus();
		} else if (Validator.isBlank(txtSDT.getText())
				|| txtSDT.getText().length() > 15) {
			check = false;
			JOptionPane.showMessageDialog(this, "Số Điện Thoại Không Hợp Lệ !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
			txtSDT.requestFocus();
		}
		return check;
	}

	private void capNhatThongTin() {
		try {
			QuanCoffee temp = (QuanCoffee) quanDao
					.findByPk(QuanCoffee.class, 1);
			if (validation()) {
				quan.setTen(txtTen.getText().trim());
				quan.setMst(txtMST.getText().trim());
				quan.setDiaChi(txtDiaChi.getText().trim());
				quan.setSdt(txtSDT.getText().trim());
				if (imgCurrent != null) {
					quan.setLogo("logo" + extension);
				} else {
					quan.setLogo(temp.getLogo());
				}

				if (quanDao.saveOrUpdate(quan)) {
					if (imgCurrent != null) {
						File f = new File(Resources_Duy.PATH_SAVE_IMAGE
								+ quan.getLogo());
						if (f.exists()) {
							f.delete();
							Common.writeImgToFile(
									imgCurrent,
									extension.substring(1),
									Resources_Duy.PATH_SAVE_IMAGE
											+ quan.getLogo());
						}
					}
					JOptionPane.showMessageDialog(this,
							"Đã Cập Nhật Thành Công !", "Thông Báo",
							JOptionPane.INFORMATION_MESSAGE);
				} else {
					fillData(temp);
					JOptionPane.showMessageDialog(this, "Đã Có Lỗi Xảy Ra !",
							"Lỗi", JOptionPane.ERROR_MESSAGE);
				}
			} else {
				fillData(temp);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}

	}

	private void processChangeImage() {
		try {
			Common.tranformJFileChooserToVi();
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.addChoosableFileFilter(new ImageFileFilter());
			fileChooser.setFileView(new ImageFileView());

			int returnVal = fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File fileImg = fileChooser.getSelectedFile();
				// imgCurrent = Common.getBufferImage(file)
				
				BufferedImage img = Common.getBufferImage(fileImg);
//				int type = img.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
//						: img.getType();
				
				if(img.getWidth() > 128 && img.getHeight() > 128)
				{
					JOptionPane.showMessageDialog(this,
							"Kích Thước Logo Không Phù Hợp !", "Thông Báo",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				
//				BufferedImage imgRe = Common.getImageResized(img, type,
//						WIDTH_LOGO, HEIGHT_LOGO);

				ImageIcon ic = Common.convertBufferImageToImageIcon(img);
				imgLogo.setIcon(ic);
				imgCurrent = img;
				extension = Common.getExtension(fileImg.getName());
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	// ================================================================

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanel(), null);
			jContentPane.add(getBtnEdit(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanel() {
		if (panel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(21, 251, 89, 25));
			jLabel3.setText("Số Điện Thoại : ");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(22, 222, 48, 25));
			jLabel2.setText("Địa Chỉ :");
			panel = new JPanel();
			panel.setLayout(null);
			panel.setBounds(new Rectangle(73, 47, 455, 294));
			Border matteBor = new MatteBorder(10,10,10,10,new Color(77, 122, 153));
			panel.setBorder(matteBor);
			panel.add(getPanelImg(), null);
			panel.add(getTxtTen(), null);
			panel.add(getTxtDiaChi(), null);
			panel.add(getTxtSDT(), null);
			panel.add(getPanelMST(), null);
			panel.add(jLabel2, null);
			panel.add(jLabel3, null);
			panel.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
				}

				public void mouseExited(MouseEvent event) {
				}
			});
		}
		return panel;
	}

	/**
	 * This method initializes panelImg
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelImg() {
		if (panelImg == null) {
			imgLogo = new JLabel();
			imgLogo.setBounds(new Rectangle(11, 36, 128, 128));
			imgLogo.setText("");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(0, 0, 150, 25));
			jLabel.setText("LOGO");
			jLabel.setFont(jLabel.getFont().deriveFont(Font.BOLD));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setOpaque(true);
			jLabel.setBackground(new Color(77, 122, 153));
			jLabel.setForeground(Color.WHITE);
			panelImg = new JPanel();
			panelImg.setLayout(null);
			panelImg.setBounds(new Rectangle(22, 20, 150, 200));
			Border borderLine = BorderFactory.createLineBorder(new Color(77,
					122, 153));
			panelImg.setBorder(borderLine);
			panelImg.add(jLabel, null);
			panelImg.add(imgLogo, null);
			panelImg.add(getJPanel(), null);
		}
		return panelImg;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(0, 174, 150, 25));
			jPanel.setOpaque(true);
			jPanel.setBackground(new Color(77, 122, 153));
			jPanel.add(getBtnChange(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes btnChange
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChange() {
		if (btnChange == null) {
			btnChange = new JButton();
			btnChange.setBounds(new Rectangle(31, 0, 88, 25));
			btnChange.setText("Thay Đổi");
			btnChange.setEnabled(false);
			btnChange.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					processChangeImage();
				}
			});
		}
		return btnChange;
	}

	/**
	 * This method initializes txtTen
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextArea getTxtTen() {
		if (txtTen == null) {
			txtTen = new JTextArea();
			txtTen.setBounds(new Rectangle(190, 19, 238, 147));
			txtTen.setOpaque(false);
			txtTen.setBackground(new Color(0, 0, 0, 0));
			txtTen.setWrapStyleWord(true);
			txtTen.setLineWrap(true);
			Border b = new MatteBorder(0, 0, 3, 0, Color.BLACK);
			txtTen.setBorder(b);
			txtTen.setEditable(false);
		}
		return txtTen;
	}

	/**
	 * This method initializes txtMST
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtMST() {
		if (txtMST == null) {
			txtMST = new JTextField();
			txtMST.setOpaque(false);
			txtMST.setBackground(new Color(0, 0, 0, 0));
			txtMST.setBounds(new Rectangle(82, 4, 154, 25));
			txtMST.setBorder(null);
			txtMST.setEditable(false);
		}
		return txtMST;
	}

	/**
	 * This method initializes txtDiaChi
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtDiaChi() {
		if (txtDiaChi == null) {
			txtDiaChi = new JTextField();
			txtDiaChi.setBounds(new Rectangle(70, 222, 360, 25));
			txtDiaChi.setOpaque(false);
			txtDiaChi.setBackground(new Color(0, 0, 0, 0));
			txtDiaChi.setBorder(null);
			txtDiaChi.setHorizontalAlignment(SwingConstants.CENTER);
			txtDiaChi.setEditable(false);

		}
		return txtDiaChi;
	}

	/**
	 * This method initializes txtSDT
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtSDT() {
		if (txtSDT == null) {
			txtSDT = new JTextField();
			txtSDT.setBounds(new Rectangle(111, 251, 318, 25));
			txtSDT.setOpaque(false);
			txtSDT.setBackground(new Color(0, 0, 0, 0));
			txtSDT.setBorder(null);
			// txtSDT.setHorizontalAlignment(SwingConstants.CENTER);
			txtSDT.setEditable(false);
		}
		return txtSDT;
	}

	/**
	 * This method initializes btnEdit
	 * 
	 * @return javax.swing.JButton
	 */
	private JToggleButton getBtnEdit() {
		if (btnEdit == null) {
			btnEdit = new JToggleButton();
			btnEdit.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "edit.png"));
			btnEdit.setToolTipText("Sửa");
			btnEdit.setBounds(new Rectangle(499, 341, 32, 32));
			btnEdit.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (btnEdit.isSelected()) {
						btnEdit.setIcon(Common
								.getImageIcon(Resources_Duy.ICON_PATH
										+ "accept.png"));
						btnEdit.setToolTipText("Chấp Nhận");

						txtTen.requestFocus();
						txtTen.setEditable(true);
						txtMST.setEditable(true);
						txtDiaChi.setEditable(true);
						txtSDT.setEditable(true);
						btnChange.setEnabled(true);

					} else {
						btnEdit.setIcon(Common
								.getImageIcon(Resources_Duy.ICON_PATH
										+ "edit.png"));
						btnEdit.setToolTipText("Sửa");

						txtTen.setEditable(false);
						txtMST.setEditable(false);
						txtDiaChi.setEditable(false);
						txtSDT.setEditable(false);
						btnChange.setEnabled(false);

						capNhatThongTin();
					}
				}
			});
		}
		return btnEdit;
	}

	/**
	 * This method initializes panelMST
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelMST() {
		if (panelMST == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(3, 4, 73, 25));
			jLabel1.setText("Mã Số Thuế :");
			panelMST = new JPanel();
			panelMST.setLayout(null);
			panelMST.setBounds(new Rectangle(190, 168, 239, 37));
			Border b = new MatteBorder(0, 0, 3, 0, Color.BLACK);
			panelMST.setBorder(b);
			panelMST.add(getTxtMST(), null);
			panelMST.add(jLabel1, null);
		}
		return panelMST;
	}

}
